package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallFormSupplyMapper;
import org.linlinjava.litemall.db.domain.LitemallFormPurchase;
import org.linlinjava.litemall.db.domain.LitemallFormSupply;
import org.linlinjava.litemall.db.domain.LitemallFormSupplyExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallSupplyAuditService {

    @Resource
    private LitemallFormSupplyMapper formSupplyMapper;

    public List<LitemallFormSupply> list(String status, String userType, String sort, String order, Integer page, Integer limit, boolean selectAll) {
        LitemallFormSupplyExample example = new LitemallFormSupplyExample();
        LitemallFormSupplyExample.Criteria criteria = example.createCriteria();


        if (!StringUtils.isEmpty(userType)){
            criteria.andUserTypeEqualTo(Byte.valueOf(userType));
        }


        if (!StringUtils.isEmpty(status)){
            criteria.andStatusEqualTo(Byte.valueOf(status));
        }

        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        if(!selectAll){
            PageHelper.startPage(page, limit);
        }

        return formSupplyMapper.selectByExample(example);
    }

    public void updateById(LitemallFormSupply purchaseExample) {

        purchaseExample.setUpdateTime(LocalDateTime.now());
        formSupplyMapper.updateByPrimaryKeySelective(purchaseExample);
    }

}
